package edu.nf.exam.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.entity.LogInfo;
import edu.nf.exam.service.LogService;
import edu.nf.exam.service.impl.LogServiceImpl;
import org.nf.mvc.annotation.RequestMapping;
import org.nf.mvc.servlet.View;
import org.nf.mvc.view.JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author : LLH
 * @create 2021/6/21
 */
public class LogController extends BaseController{

    /**
     * 添加日志
     * @return
     */
    @RequestMapping("/log/add")
    public View add(LogInfo logInfo){
        LogService logService = new LogServiceImpl();
        try {
            logService.checkAdd(logInfo);

            return new JsonView(success());
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 查询所有日志
     * @param logInfo
     * @return
     */
    @RequestMapping("/log/selectAll")
    public View selectAll(LogInfo logInfo){
        LogService logService=new LogServiceImpl();
        try {
            List<LogInfo> list = logService.checkSelectAll(logInfo);

            JsonView jsonView = new JsonView(success(list));
            jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
            return jsonView;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     *分页查询所有日志
     * @param userId 查询条件 ，根据用户ID查询
     * @param userName  查询条件 ，根据用户名查询
     * @param page  当前页 layui传过来的固定分页参数，等同于pageNum
     * @param limit 每页记录数  layui传过来的固定分页参数，等同于pageSize
     * @return
     */
    @RequestMapping("/log/selectByPage")
    public View selectByPage(Integer userId, String userName,Integer page,Integer limit){
        LogService logService=new LogServiceImpl();
        PageInfo<LogInfo> pageInfo = logService.selectByPage(userId,userName,page,limit);

        JsonView jsonView = new JsonView(page(pageInfo));
        jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return jsonView;
    }

    /**
     * 根据日期查询日志数
     * @param request
     * @return
     */
    @RequestMapping("/log/selectActiceDegree")
    public View selectActiceDegree(HttpServletRequest request){
        String[] dateArrs = request.getParameterValues("dateArr");

        LogService service=new LogServiceImpl();
        // 定义一个空数组，用于接收本月每天的日志动态数量
        int[] arr=new int[dateArrs.length];

        try {
            for (int i=0;i<dateArrs.length;i++) {
                List<LogInfo> list = service.selectByCreateTime(dateArrs[i]);
                arr[i]=list.size();
            }
            return new JsonView(success(arr));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }
}
